from PySide6 import QtWidgets
from PySide6.QtGui import QStandardItemModel

class MainTableWidget(QtWidgets.QTableView):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.model = QStandardItemModel(0, 6)
        self.model.setHorizontalHeaderLabels(['id', '条码', '名称', '货号', 'SKU', '包装', '总库存'])
        self.setModel(self.model)
        self.setColumnWidth(0, 100)
        self.setColumnWidth(1, 150)
        self.setColumnWidth(2, 250)
        self.setColumnWidth(3, 150)
        self.setColumnWidth(4, 150)
        self.setColumnWidth(5, 50)
        self.setColumnWidth(6, 100)
        self.setEditTriggers(self.EditTrigger.NoEditTriggers)
    
    def set_row(self, row, data):
        for index, item in enumerate(data):
            self.model.setData(self.model.index(row, index), item)

    def update_model(self, list):
        self.model.setRowCount(len(list))
        for index, item in enumerate(list):
            self.set_row(index, [
                item['id'],
                item['sn'],
                item['name'],
                item['item_ref'],
                item['sku'],
                item['pack'],
                item['warehouse']
            ])